package org.erepublik.company.service;

import java.util.ArrayList;
import java.util.Arrays;

import org.erepublik.company.ECountry;
import org.erepublik.company.comparators.HouseComparatorByWellness;
import org.erepublik.company.product.House;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONHouseService extends JSONProductService<House> implements IHouseService {

    public JSONHouseService() {
        super("house");
    }

    @Override
    protected House[] getEmptyTable() {
        return new House[0];
    }

    @Override
    protected House[] getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        House[] ticket = getHouseFromJSONArray(jsonArrayWithFood).toArray(new House[0]);
        Arrays.sort(ticket, new HouseComparatorByWellness());
        return ticket;
    }
    

    public static ArrayList<House> getHouseFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        ArrayList<House> ticket = new ArrayList<House>();
        for (int i = 0; i < jsonArrayWithFood.length(); i++) {
            ticket.add (  House.getHouseFromJSON( (JSONObject)jsonArrayWithFood.get(i) ) );
        }
        return ticket;
    }
    

    @Override
    public House[] getHouses(ECountry country) {
        update();
        if (country==null)
            return allProducts;
        return productMap.get(country);
    }

    @Override
    public String getMinutesSinceHouseUpdate() {

        return getMinutesSinceUpdate();
    }

}
